﻿Public Class CreateFile



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
        If System.IO.File.Exists(TextBox1.Text) = True Then
            Dim name = System.IO.Path.GetFileName(TextBox1.Text)
            Try
                System.IO.File.Copy(TextBox1.Text, path + name, True)
                Dim type = System.IO.Path.GetExtension(TextBox1.Text)
                If type = ".html" Or type = ".css" Or type = ".js" Or type = ".txt" Then
                    Dim editor As New TabPage
                    editor.Text = name
                    editor.Name = name
                    Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                    If My.Settings.linenumbers = True Then
                        htmlbox.ShowLineNumbers = True
                    Else
                        htmlbox.ShowLineNumbers = False
                    End If
                    If type = ".html" Then
                        htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                    ElseIf type = ".js" Then
                        htmlbox.Language = FastColoredTextBoxNS.Language.JS
                    Else
                        htmlbox.Language = FastColoredTextBoxNS.Language.Custom
                    End If
                    htmlbox.ContextMenuStrip = GeneralForm.ContextMenuStrip1
                    Dim reader As New System.IO.StreamReader(path + name, System.Text.Encoding.GetEncoding(1251))
                    htmlbox.Text = reader.ReadToEnd
                    reader.Close()
                    htmlbox.Dock = DockStyle.Fill
                    editor.Controls.Add(htmlbox)
                    GeneralForm.tabcontrol.TabPages.Add(editor)
                    GeneralForm.tabcontrol.SelectedTab = editor
                    GeneralForm.refreshfiles()
                ElseIf type = ".jpg" Or type = ".png" Or type = ".bmp" Or type = ".gif" Then
                    ImageViewer.PictureZoom1.Load(path + name)
                    If type = ".png" Then
                        ImageViewer.PictureZoom1.BackgroundImage = My.Resources.bg
                    Else
                        ImageViewer.PictureZoom1.BackgroundImage = Nothing
                    End If
                    ImageViewer.Text = path + name
                    ImageViewer.Show()
                End If
                    Me.Close()

            Catch ex As Exception
                MsgBox("File was not copied! An error occured: " + ex.Message, 48, "Error!")
                Exit Sub
            End Try
        Else
            If TextBox1.Text = "" Then
                MsgBox("You have to fill in file name!", 64, "Error!")
            ElseIf ComboBox1.SelectedItem = Nothing Then
                MsgBox("You have to choose file type!", 64, "Error!")
            ElseIf Not TextBox1.Text = "" And Not ComboBox1.SelectedItem = Nothing Then
                ' Узнаём тип файла
                Dim typecombo As String = ""
                If ComboBox1.SelectedItem = "Web page(*.html)" Then
                    typecombo = ".html"
                ElseIf ComboBox1.SelectedItem = "JavaScript(*.js)" Then
                    typecombo = ".js"
                ElseIf ComboBox1.SelectedItem = "CSS(*.css)" Then
                    typecombo = ".css"
                ElseIf ComboBox1.SelectedItem = "Text File(*.txt)" Then
                    typecombo = ".txt"
                ElseIf ComboBox1.SelectedItem = "VBScript(*.vbs)" Then
                    typecombo = ".vbs"
                End If
                If System.IO.File.Exists(path + TextBox1.Text + typecombo) = True Then
                    MsgBox("File with this name already exists!!", 48, "Error!")
                Else
                    Try
                        Dim writer As New System.IO.StreamWriter(path + TextBox1.Text + typecombo)
                        If typecombo = ".html" Then
                            writer.Write(My.Settings.htmlsyntax)
                        Else
                            writer.Write("")
                        End If
                        writer.Close()
                        Dim editor As New TabPage
                        editor.Text = TextBox1.Text + typecombo
                        editor.Name = TextBox1.Text + typecombo
                        Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                        If typecombo = ".html" Then
                            htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                        ElseIf typecombo = ".js" Then
                            htmlbox.Language = FastColoredTextBoxNS.Language.JS
                        ElseIf typecombo = ".vbs" Then
                            htmlbox.Language = FastColoredTextBoxNS.Language.VB
                        Else
                            htmlbox.Language = FastColoredTextBoxNS.Language.Custom
                        End If
                        htmlbox.ContextMenuStrip = GeneralForm.ContextMenuStrip1
                        Dim reader As New System.IO.StreamReader(path + TextBox1.Text + typecombo, System.Text.Encoding.GetEncoding(1251))
                        htmlbox.Text = reader.ReadToEnd
                        reader.Close()
                        htmlbox.Dock = DockStyle.Fill
                        editor.Controls.Add(htmlbox)
                        GeneralForm.tabcontrol.TabPages.Add(editor)
                        GeneralForm.tabcontrol.SelectedTab = editor
                        GeneralForm.refreshfiles()
                        Me.Close()
                    Catch ex As Exception
                        MsgBox("While file was creating, an error occured: " + ex.Message, 16, "Error")
                        Exit Sub
                    End Try
                End If
            End If
        End If
        GeneralForm.refreshfiles()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If System.IO.File.Exists(TextBox1.Text) = True Then
            ComboBox1.Enabled = False
            Button1.Text = "Add existing"
        Else
            Button1.Text = "Create"
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = ofd.FileName
        End If
    End Sub
End Class